home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / unix / arcunx11 / arc.sh2 / arcmisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-01  |  5.7 KB  |  198 lines

  1. /*
  2.  *    arcmisc.c    1.1
  3.  *
  4.  *    Author: Thom Henderson
  5.  *    Original System V port: Mike Stump
  6.  *    Enhancements, Bug fixes, and cleanup: Chris Seaman
  7.  *    Date: Fri Mar 20 09:57:02 1987
  8.  *    Last Mod.    3/21/87
  9.  *
  10.  */
  11.  
  12. /*
  13.  * ARC - Archive utility - ARCMISC
  14.  * 
  15.  * Description:
  16.  *      This file contains miscellaneous routines for string
  17.  *      management, file management, and program control.
  18.  */
  19.  
  20. #include "arc.h"
  21. #include <ctype.h>
  22.  
  23. INT rempath(nargs,arg)               /* remove paths from filenames */
  24. INT nargs;                           /* number of names */
  25. char *arg[];                         /* pointers to names */
  26. {
  27.     char *i, *strrchr();             /* string index, reverse indexer */
  28.     INT n;                           /* index */
  29.  
  30.     for(n=0; n<nargs; n++)           /* for each supplied name */
  31.     {
  32.         i=strrchr(arg[n],'/');       /* search for end of path */
  33.         if(i)                        /* if path was found */
  34.             arg[n] = i+1;            /* then skip it */
  35.     }
  36. }
  37.  
  38. /* make a file name using a template */
  39. char *makefnam(rawfn,template,result)
  40. unsigned char *rawfn;                /* the original file name */
  41. unsigned char *template;             /* the template data */
  42. unsigned char *result;               /* where to place the result */
  43. {
  44.     char *arc_ext = ".arc";          /* possible existing extension */
  45.     char *i, *strrchr();             /* string indexing stuff */
  46.  
  47.     i = strrchr(rawfn,'.');          /* strip 'arc' extension from filename */
  48.     if (strcmp(i,arc_ext) == 0) *i = '\0';
  49.  
  50.     strncpy(result,rawfn,10);        /* rebuild it using supplied template */
  51.     strcat(result,template);
  52.     return((char *)&result[0]);
  53. }
  54.  
  55. /*  convert a string to upper case  */
  56. upper(s)
  57. char *s;
  58. {
  59.     while (*s = toupper(*s)) ++s;
  60. }
  61.  
  62. setmem(dest,size,c)
  63. char *dest,c;
  64. INT size;
  65. {
  66.     int i;
  67.  
  68.     for (i = 0; i < size; dest[i] = c, i++);
  69. }
  70.  
  71. abort(s,arg1,arg2,arg3)                /* something went wrong...QUIT!! */
  72. char *s;
  73. {
  74.     char tempname1[STRLEN], tempname2[STRLEN];
  75.  
  76.     sprintf(tempname1,"%s.crn",arctemp);
  77.     sprintf(tempname2,"%s.cvt",arctemp);
  78.  
  79.     unlink(bakname);                   /* remove all possible temp files */
  80.     unlink(newname);
  81.     unlink(tempname1);
  82.     unlink(tempname2);
  83.  
  84.     fprintf(stderr,"arc: ");           /* explain things to the user */
  85.     fprintf(stderr,s,arg1,arg2,arg3);
  86.     fprintf(stderr,"\n");
  87.     exit(1);                           /* quit */
  88. }
  89.  
  90. rename(o, n)
  91. char *o, *n;
  92. {
  93.     return(link(o, n) || unlink(o));
  94. }
  95.  
  96. makenames(rawfn)
  97. char *rawfn;
  98. {
  99.     char pathtemp[STRLEN];             /* temporary path holder */
  100.     char nametemp[STRLEN];             /* temporary arcname holder */
  101.     char *buf;                         /* temporary pointer */
  102.     char *i, *strrchr();               /* string indexing junk */
  103.     long getpid();                     /* process id function */
  104.  
  105.     strcat(pathtemp,rawfn);
  106.     if (i = strrchr(buf=rawfn,'/'))    /* if names are part of paths */
  107.     {                                  /* lots to do */
  108.         buf=i+1;
  109.         pathtemp[strlen(rawfn)-strlen(buf)]='\0';
  110.  
  111.         makefnam(buf,".arc",nametemp); /* make 'arcname' */
  112.         sprintf(arcname,"%s%s",pathtemp,nametemp);
  113.  
  114.         makefnam(buf,".bak",nametemp); /* make 'bakname' */
  115.         sprintf(bakname,"%s%s",pathtemp,nametemp);
  116.  
  117.         sprintf(arctemp,"%s.Arc%ld",pathtemp,getpid());
  118.     }
  119.     else                               /* not so much to do */
  120.     {
  121.         makefnam(rawfn,".arc",arcname);
  122.         makefnam(rawfn,".bak",bakname);
  123.  
  124.         sprintf(arctemp,".Arc%ld",getpid());
  125.     }
  126.     sprintf(newname,"%s.arc",arctemp);
  127. }
  128.  
  129. onintr()                               /* SIGNAL was caught */
  130. {
  131.     abort("User Requested Abort");
  132. }
  133.  
  134. /*
  135.  * This function sorts the command line file arguments.  Needed since
  136.  * the add, update, etc., function does no sorting, and could result in
  137.  * multiple archive entries for the same file name.
  138.  */
  139. sortarg(num,arg)                       /* sort argument list, remove dups */
  140. int num;
  141. char *arg[];
  142. {
  143.     char *temp;                        /* temporary pointer */
  144.     INT top, seek;                     /* placeholders for sorting */
  145.     INT dups = 0;                      /* how many duplicates are there */
  146.     char *strrchr(), *i;               /* string indexing stuff */
  147.     char *buf1, *buf2;                 /* pointers for strcmp to use */
  148.  
  149.     /* sort the arguments, ignoring pathnames */
  150.  
  151.     for (top = 0;top < num-1;top++)
  152.         for (seek = top+1;seek<num;seek++)
  153.         {
  154.             buf1 = arg[top];
  155.             buf2 = arg[seek];
  156.             if (i = strrchr(arg[top],'/')) buf1 = i + 1;
  157.             if (i = strrchr(arg[seek],'/')) buf2 = i + 1;
  158.             if (strcmp(buf1,buf2) > 0)
  159.             {
  160.                 temp = arg[top];
  161.                 arg[top] = arg[seek];
  162.                 arg[seek] = temp;
  163.             }
  164.         }
  165.  
  166.     /* find any occurences of 'arcname', and remove them */
  167.  
  168.     for (top = 0;top < num;top++)
  169.         while (strcmp(arg[top],arcname) == 0)
  170.         {
  171.             for (seek = top; seek < num;seek++)
  172.                 arg[seek] = arg[seek + 1];
  173.             arg[--num] = '\0';
  174.             dups++;
  175.         }
  176.  
  177.     /* find any other duplicate arguments (ignoring pathnames), */
  178.     /* and remove the second and subsequent occurences */
  179.  
  180.     for (top = 0;top < num-1;top++)
  181.     {
  182.         buf1 = arg[top];
  183.         buf2 = arg[top + 1];
  184.         if (i = strrchr(arg[top],'/')) buf1 = i + 1;
  185.         if (i = strrchr(arg[top + 1],'/')) buf2 = i + 1;
  186.         while (strcmp(buf1,buf2) == 0)
  187.         {
  188.             for (seek = top + 1;seek < num;seek++)
  189.                 arg[seek] = arg[seek + 1];
  190.             arg[--num] = '\0';
  191.             buf2 = arg[top + 1];
  192.             if (i = strrchr(arg[top + 1],'/')) buf2 = i + 1;
  193.             dups++;
  194.         }
  195.     }
  196.     return(dups);              /* tell main() how many we found */
  197. }
  198.